// 引入接口文件
import {getProduct,regionArea,getReport,getChart,getContrast,down} from './codeChannelCliqueServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import { makeAddress ,download} from "utils/util"
// import { SIGUNUSED } from 'constants';

// 初始默认状态
const defultState = {
	area1:[],
	products:[],
	areaList:[],
	reports:[],
	showModal:false,
	loading:true,
	params:{
		tableRowType:null,
		productId:null,
		cityCode:[]
	},
	query1:{
		dataType:1,
		contrastType:null,
		contrastData:[],
		statisticsContent:[],
		productId: null,
		regionId: null
	},
	time:{
		startTime:null,
		endTime:null
	},
	time1:{
		startTime:null,
		endTime:null
	},
	dateList:[],
	name:[],
	dataList:[],
	visible:false,
	contrast:[],
	params1:{
		productId:null,
		contrastData:null,
		contrastType:null,
	},
	roleType:'',
	statisticsContent:null,
	contrastType:null,
	regionId:null,
	productId:null,
	selectValue:[],
	title:null,
	selected:'7',
	selected2:'7',
	graphRespDtos:['']
}

// 声明module
const tmpModule = {
	// 默认状态
	state: cloneDeep(defultState),
	// 入口函数(先执行)
	subscriptions: {
		setup({ dispatch, history }) {
			history.listen(({ pathname, query }) => {
				// 页面路由匹配时执行
				if ('' + tmpModule.pathname === '' + pathname) {
					let time={
						startTime:moment().subtract(6, 'days').format('YYYY-MM-DD'),
						endTime:moment().format('YYYY-MM-DD')
					}
					let params1={
						contrastData:query.statisticsContent,
						productId:null,
						contrastType:query.contrastType,
					}
					let cityCode=[]
					if(query.provinceCode){
						cityCode[0]=query.provinceCode
						if(query.cityCode){
							cityCode[1]=query.cityCode
						}
					}
					let params={
						tableRowType:query.statisticsContent,
						productId:null,
						cityCode
					}
					dispatch({ type: 'updateStore', payload: {...cloneDeep(defultState),roleType:localStorage.getItem('roleType'),time,time1:time,params1,params} })
					// dispatch({ type: 'getAreaList', payload: {} });
					dispatch({type:'regionArea',payload:{}})
					dispatch({type:'getReport',payload:{}})
					dispatch({type:'getChart',payload:{}})
				}
			})
		},
	},
	// 方法列表
	effects: {
		//获取区域
		*regionArea({payload},{put,call,select}){
			let {params}=yield select(state => state[tmpModule.namespace])
			const {success,data}=yield call(regionArea)
			let regionId=null,productId=null,products=[]
			if(success){
				if(data.length==1){
					if(data[0].products.length==1){
						products=data[0].products
						regionId=data[0].value
						productId=data[0].products[0].value
					}
				}
				if(params.tableRowType){
					data.map((item,index)=>{
						if(params.tableRowType==item.value){
							products=item.products
						}
					})
				}
				yield put({type:'updateStore',payload:{area1:data,products,regionId,productId}})
			}
		},
		*getProduct({payload},{put,call,select}){
			let {params,products} = yield select(state => state[tmpModule.namespace])
			if(params.cityCode.length>1){
				const { data, success } = yield call(getProduct,{cityCode:params.cityCode[1]});
				if(success){
					products=data
					yield put({ type: 'updateStore',payload:{products}})
				}
			}
		  },
		//获取类别渠道汇总
		*getReport({payload},{put,call,select}){
			let { params,query1,time1}=yield select(state => state[tmpModule.namespace])
			let query={...query1,...time1,...payload,provinceCity:params.cityCode}
			delete query.contrastData
			if(localStorage.getItem('roleType')==2 && params.tableRowType){
				query.statisticsContent[0]=params.tableRowType
				console.log(query)
			}
			let {success,data}=yield call(getReport,query)
			if(success){
				yield put({type:'updateStore',payload:{reports:data,loading:false,time1}})
			}
		},
		*getChart({payload},{put,call,select}){
			let {params, dateList,name,dataList,query1,params1,time,title,graphRespDtos,contrastType,statisticsContent}=yield select(state => state[tmpModule.namespace])
			let selectValue=[]
			console.log(params1,params1.contrastData,params,query1)
			let query={...query1,...time,...payload,provinceCity:params.cityCode}
			if(localStorage.getItem('roleType')==2){
				// if(params.tableRowType){
				// 	query.statisticsContent[0]=params.tableRowType
				// }
				if(params1.contrastData){
					query.statisticsContent[0]=params1.contrastData
					query.regionId=params1.contrastData
				}
				if(params.tableRowType){
					query.statisticsContent[0]=params.tableRowType
					query.regionId=params.tableRowType
				}
				
				console.log(query)
			}
			let {data,success}=yield call(getChart,query)
			if(success){
				dateList=[],
				dataList=[],
				name=[]
				if(data){
					params1.contrastData=data.contrastData || null
					params1.contrastType=data.contrastType || null,
					query1.contrastType=data.contrastType || null,
					title=data.title
				}else{
					params1.contrastData=null
					params1.contrastType=null
				}
				if(data && data.graphRespDtos){
					graphRespDtos=data.graphRespDtos
					for(let i in data.graphRespDtos){
						let color=function (){ 
							return "#"+("00000"+((Math.random()*16777215+0.5)>>0).toString(16)).slice(-6); 
						}
						let obj={
							name:'',
							data:[],
							type:'line',
							symbol: 'circle',
							smooth: true,        
							symbolSize: 3,
							// itemStyle: {                    
							// 	normal: {                        
							// 		color:color,
							// 		borderColor:color
							// 	 }                
							// },
						}
						obj.name=data.graphRespDtos[i].graphName
						name.push(data.graphRespDtos[i].graphName)
						if(data.graphRespDtos[i].graphType!=-1){
							selectValue.push(data.graphRespDtos[i].graphType)
						}
						data.graphRespDtos[i].graphValues.map((item,index)=>{
							if(i==0){
								dateList.push(item.time)
							}
							obj.data.push(item.value)
						})
						dataList.push(obj)
					}
				}
				
			}
			yield put({type:'updateStore',payload:{dataList,name,dateList,loading:false,params1,selectValue,title,graphRespDtos,time,query1}})
		},
		*getContrast({payload},{put,call,select}){
			let {params1,params,regionId}=yield select(state => state[tmpModule.namespace])
			let {data,success}=yield call(getContrast,{...params1,provinceCity:params.cityCode,regionId})
			if(success){
				if(data){
					yield put({type:'updateStore',payload:{contrast:data}})
				}
			}
		},
		*down({payload},{put ,call,select}){
			let { params,query1,time1}=yield select(state => state[tmpModule.namespace])
			let query={...query1,...params,...time1,}
			if(!query.startTime){
				query.startTime=moment().subtract(6, 'days').format('YYYY-MM-DD')
				query.endTime=moment().format('YYYY-MM-DD')
			}
			let {data,success}=yield call(down,query)
			if(success){
				download(data)
			}
		}
	},
	// 存入状态机
	reducers: {
		updateStore(preState, action) {
			return Object.assign({}, preState, action.payload)
		},
	}
}
export default tmpModule
